
@if /i {%1}=={check} (
  @shift
  @goto check_build_log
)

@if {%1}=={} (
   @SET MSVC_BUILD=build
) else (
   @SET MSVC_BUILD=%1
)


:Main

  @if not defined PROJECT_DEBUG SET PROJECT_DEBUG=debug

  @if /i not {%PROJECT_DEBUG%}=={all}  goto :LB_BUILD_ALL
  
  @SET PROJECT_DEBUG=debug  
  @call :LB_BUILD_ALL
  
  @SET PROJECT_DEBUG=release
  @call :LB_BUILD_ALL
  
  @SET PROJECT_DEBUG=all
  
@goto :EOF

:LB_BUILD_ALL
  
  @if defined   MSVC_PROJECT_ROOT pushd %MSVC_PROJECT_ROOT%

  @for %%i in (%MSVC_PROJECTS%) do @call :LB_BUILD %%i

  @if defined   MSVC_PROJECT_ROOT popd
  
@goto :EOF

:LB_BUILD

  @for /F "tokens=1,2 delims=:" %%i in ('echo %1') do @(
    @echo %MSVC_BUILD% %%i[%%j]
    @%MSVC_DEVENV% %%i /%MSVC_BUILD% %PROJECT_DEBUG% /project %%j /out build.log
  )

@goto :EOF


:check_build_log
   
  @echo begin log checking

  @if defined   MSVC_PROJECT_ROOT pushd %MSVC_PROJECT_ROOT%

  @if not exist build.log goto :check_build_log_end

  @set  NUM_ERROR=0

  @for /F "tokens=1,2,3,4,5,6,7,8 delims=-=, " %%i in ('type build.log') do @(
     @if /i {%%j}=={Rebuild} @( 
           @SET CUR_PRJ=%%n
           @SET CUR_CFG=%%p
     )

     @if /i {%%i}=={Rebuild} @call :CHECK_LINE %%k %%l %%m %%n %%o %%p
  )

  @if {%NUM_ERROR%}=={0} @(
     @echo build success
  ) else @(
     @echo error count = %NUM_ERROR%
     @notepad build.log
  )

:check_build_log_end

  @SET CUR_PRJ=
  @SET CUR_CFG=
  @SET NUM_ERROR=
  
  @if defined   MSVC_PROJECT_ROOT popd

  @echo end check

@goto :EOF

:CHECK_LINE
  @if {%3}=={0} goto :EOF
  @SET /A NUM_ERROR=%NUM_ERROR% + 1
@goto :EOF
